
using System;
using System.Data;
using System.Linq;
using System.Linq.Dynamic;
using System.Linq.Dynamic.Core;
using System.Linq.Expressions;
using System.Threading.Tasks;
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore;

using Abp.UI;
using Abp.AutoMapper;
using Abp.Extensions;
using Abp.Authorization;
using Abp.Domain.Repositories;
using Abp.Application.Services.Dto;
using Abp.Linq.Extensions;


using Yozeev.BusinessLogic;
using Yozeev.BusinessLogic.Dtos;
using Yozeev.BusinessLogic.DomainService;
using Yozeev.BusinessLogic.Authorization;
using Yozeev.Common;
using Abp.Authorization.Users;

namespace Yozeev.BusinessLogic
{
    /// <summary>
    /// TicketIntroduce应用层服务的接口实现方法  
    ///</summary>
    [AbpAuthorize]
    public class TicketIntroduceAppService : YozeevAppServiceBase, ITicketIntroduceAppService
    {
        private readonly IRepository<TicketIntroduce, Guid> _entityRepository;

		private readonly IRepository<Ticket, Guid> _ticketRepository;

		private readonly ITicketIntroduceManager _entityManager;

        private readonly IQueryHelper _queryHelper;

        /// <summary>
        /// 构造函数 
        ///</summary>
        public TicketIntroduceAppService(
        IRepository<TicketIntroduce, Guid> entityRepository
        ,ITicketIntroduceManager entityManager
			, IRepository<Ticket, Guid> ticketRepository
            , IQueryHelper queryHelper
        )
        {
            _entityRepository = entityRepository; 
             _entityManager=entityManager;
			_ticketRepository = ticketRepository;
            _queryHelper = queryHelper;
        }


        /// <summary>
        /// 获取TicketIntroduce的分页列表信息
        ///</summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<PagedResultDto<TicketIntroduceListDto>> GetPaged(GetTicketIntroducesInput input)
		{

            var expression = _queryHelper.WhereExp<TicketIntroduce>(input.queryData);
			if (AbpUserBase.AdminUserName != GetCurrentUser().UserName)
				expression = _queryHelper.SetWhereExp<TicketIntroduce>(expression, GetViewTypeEnum<TicketIntroduce>(), await GetBranchUser());
			var query = from ticketIntroduce in _entityRepository.GetAll().AsNoTracking()
                        .Where(expression)
                        select new 
						{
                            ticketIntroduce,
                            ticketIntroduce.Ticket,
                            ticketIntroduce.ScenicSpot
                        };
            var list = new List<TicketIntroduce>();
            query.ToList().ForEach(temp => {
                var _ticketIntroduce = temp.ticketIntroduce;
                _ticketIntroduce.Ticket = temp.Ticket;
                _ticketIntroduce.ScenicSpot = temp.ScenicSpot;
                list.Add(_ticketIntroduce);
            });
            // TODO:根据传入的参数添加过滤条件
            var count = list.Count();
			list = list.AsQueryable().OrderBy(input.Sorting)
					.PageBy(input)
					.ToList();
			var entityList = list;
   //         var count = await query.CountAsync();

			//var entityList = await query
			//		.OrderBy(input.Sorting).AsNoTracking()
			//		.PageBy(input)
			//		.ToListAsync();

			// var entityListDtos = ObjectMapper.Map<List<TicketIntroduceListDto>>(entityList);
			var entityListDtos =entityList.MapTo<List<TicketIntroduceListDto>>();

			return new PagedResultDto<TicketIntroduceListDto>(count,entityListDtos);
		}


		/// <summary>
		/// 通过指定id获取TicketIntroduceListDto信息
		/// </summary>
		public async Task<TicketIntroduceListDto> GetById(EntityDto<Guid> input)
		{
			var entity = await _entityRepository.GetAsync(input.Id);

		    return entity.MapTo<TicketIntroduceListDto>();
		}

		/// <summary>
		/// 获取编辑 TicketIntroduce
		/// </summary>
		/// <param name="input"></param>
		/// <returns></returns>
		public async Task<GetTicketIntroduceForEditOutput> GetForEdit(NullableIdDto<Guid> input)
		{
			var output = new GetTicketIntroduceForEditOutput();
TicketIntroduceEditDto editDto;

			if (input.Id.HasValue)
			{
				var entity = await _entityRepository.GetAsync(input.Id.Value);

				editDto = entity.MapTo<TicketIntroduceEditDto>();

				//ticketIntroduceEditDto = ObjectMapper.Map<List<ticketIntroduceEditDto>>(entity);
			}
			else
			{
				editDto = new TicketIntroduceEditDto();
			}

			output.TicketIntroduce = editDto;
			return output;
		}


		/// <summary>
		/// 添加或者修改TicketIntroduce的公共方法
		/// </summary>
		/// <param name="input"></param>
		/// <returns></returns>
		public async Task CreateOrUpdate(CreateOrUpdateTicketIntroduceInput input)
		{

			if (input.TicketIntroduce.Id.HasValue)
			{
				await Update(input.TicketIntroduce);
			}
			else
			{
				await Create(input.TicketIntroduce);
			}
		}


		/// <summary>
		/// 新增TicketIntroduce
		/// </summary>
		protected virtual async Task<TicketIntroduceEditDto> Create(TicketIntroduceEditDto input)
		{
			//TODO:新增前的逻辑判断，是否允许新增

            // var entity = ObjectMapper.Map <TicketIntroduce>(input);
            var entity=input.MapTo<TicketIntroduce>();
			

			entity = await _entityRepository.InsertAsync(entity);
			return entity.MapTo<TicketIntroduceEditDto>();
		}

		/// <summary>
		/// 编辑TicketIntroduce
		/// </summary>
		protected virtual async Task Update(TicketIntroduceEditDto input)
		{
			//TODO:更新前的逻辑判断，是否允许更新

			var entity = await _entityRepository.GetAsync(input.Id.Value);
			input.MapTo(entity);

			// ObjectMapper.Map(input, entity);
		    await _entityRepository.UpdateAsync(entity);
		}



		/// <summary>
		/// 删除TicketIntroduce信息的方法
		/// </summary>
		/// <param name="input"></param>
		/// <returns></returns>
		public async Task Delete(EntityDto<Guid> input)
		{
			//TODO:删除前的逻辑判断，是否允许删除
			await _entityRepository.DeleteAsync(input.Id);
		}



		/// <summary>
		/// 批量删除TicketIntroduce的方法
		/// </summary>
		public async Task BatchDelete(List<Guid> input)
		{
			// TODO:批量删除前的逻辑判断，是否允许删除
			await _entityRepository.DeleteAsync(s => input.Contains(s.Id));
		}


		/// <summary>
		/// 导出TicketIntroduce为excel表,等待开发。
		/// </summary>
		/// <returns></returns>
		//public async Task<FileDto> GetToExcel()
		//{
		//	var users = await UserManager.Users.ToListAsync();
		//	var userListDtos = ObjectMapper.Map<List<UserListDto>>(users);
		//	await FillRoleNames(userListDtos);
		//	return _userListExcelExporter.ExportToFile(userListDtos);
		//}

    }
}


